package com.china08.yxyapi.service.manage.tec.teaching.lecture.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingNoteTaking;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingNoteTaking.Note;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingPerformance;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingReport;
import com.china08.yxyapi.entity.mg.esbag.test.YxyEsbagTeachingTestQues;
import com.china08.yxyapi.entity.mg.esbag.test.YxyEsbagTeachingTestQuesGroup;
import com.china08.yxyapi.entity.mg.esbag.test.YxyEsbagTeachingTestQuesGroupStu;
import com.china08.yxyapi.entity.mg.esbag.test.YxyEsbagTeachingTestQuesStu;
import com.china08.yxyapi.entity.mg.esbag.testreport.YxyEsbagTeachingTestAnalyseKnow;
import com.china08.yxyapi.entity.mg.esbag.testreport.YxyEsbagTeachingTestAnalyseKnow.Know;
import com.china08.yxyapi.entity.mg.esbag.testreport.YxyEsbagTeachingTestAnalyseScore;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingLatestVersionRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingNoteTakingRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingPerformanceRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingReportRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestAnalyseKnowRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestAnalyseScoreRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestQuesGroupRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestQuesGroupStuRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestQuesRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestQuesStuRepository;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.LectureReportClassImportService;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.LectureReportClassJudgeService;
import com.china08.yxyapi.util.YXCSPUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 备授课--授课报告--导入分析
 * 
 * @author ygl
 *
 */
@Service
public class LectureReportClassImportServiceImpl implements LectureReportClassImportService {

    public static Logger log = Logger.getLogger(LectureReportClassImportServiceImpl.class);

    @Autowired
    private YxyEsbagTeachingReportRepository yxyEsbagTeachingReportRepository;

    @Autowired
    private YxyEsbagTeachingTestAnalyseKnowRepository yxyEsbagTeachingTestAnalyseKnowRepository;

    @Autowired
    private YxyEsbagTeachingTestAnalyseScoreRepository yxyEsbagTeachingTestAnalyseScoreRepository;

    @Autowired
    private YxyEsbagTeachingNoteTakingRepository yxyEsbagTeachingNoteTakingRepository;

    @Autowired
    private YxyEsbagTeachingPerformanceRepository yxyEsbagTeachingPerformanceRepository;

    @Autowired
    private YxyEsbagTeachingTestQuesRepository esbagTeachingTestQuesRepository;

    @Autowired
    private YxyEsbagTeachingTestQuesGroupRepository esbagTeachingTestQuesGroupRepository;

    @Autowired
    private YxyEsbagTeachingTestQuesGroupStuRepository yxyEsbagTeachingTestQuesGroupStuRepository;

    @Autowired
    private YxyEsbagTeachingTestQuesStuRepository yxyEsbagTeachingTestQuesStuRepository;

    @Autowired
    private LectureReportClassJudgeService lectureReportClassJudgeService;

    @Autowired
    private YXCSPUtil yxcspUtil;

    @Autowired
    private YxyEsbagTeachingLatestVersionRepository latestVersionRepository;

    @Override
    public void importFile(String data) throws IOException {

        // 解决UTF-8编码不能解析(去除编码中第一个隐形字符)
        data = data.substring(1, data.length());
        // 日志输出
        log.info("开始解析文件内容....");
        // 解析数据
        JSONArray jsonArray = JSONArray.fromObject(data);
        // 获取数组长度
        Integer dataSize = jsonArray.size();

        Assert.isTrue(dataSize == 10, "文件内容错误,无法解析");
        // 解析版本
        JSONObject versionData = jsonArray.getJSONObject(0);
        String string = versionData.toString();
        JSONObject jsonObject = JSONObject.fromObject(string);
        // 获取版本对象
        String objString = jsonObject.getString("versionData");
        JSONArray versionDataArray = JSONArray.fromObject(objString);
        String userTime = "";
        String packId = "";
        // 获取对象属性并存入数据库
        if (versionDataArray.size() > 0) {
            packId = versionDataArray.getJSONObject(0).getString("packId");
            String id = versionDataArray.getJSONObject(0).getString("_id");
            String latestVersion = versionDataArray.getJSONObject(0).getString("latestVersion");
            userTime = versionDataArray.getJSONObject(0).getString("userTime");
        }

        log.info("解析版本内容成功,开始解析基本数据...");

        // 解析基本数据
        JSONObject baseData = jsonArray.getJSONObject(1);
        JSONObject baseDataoObject = baseData.getJSONObject("baseData");
        String bookcontentsId = baseDataoObject.getString("bookcontentsId");
        String phaseId = baseDataoObject.getString("phaseId");
        String semesterName = baseDataoObject.getString("semesterName");
        String className = baseDataoObject.getString("className");
        String bookcontentsName = baseDataoObject.getString("bookcontentsName");
        String versionName = baseDataoObject.getString("versionName");
        String userId = baseDataoObject.getString("userId");
        String subjectId = baseDataoObject.getString("subjectId");
        String semesterId = baseDataoObject.getString("semesterId");
        String versionId = baseDataoObject.getString("versionId");
        String classId = baseDataoObject.getString("classId");
        String schoolNick = baseDataoObject.getString("schoolNick");
        String userNick = baseDataoObject.getString("userNick");
        String schoolId = baseDataoObject.getString("schoolId");
        String share = baseDataoObject.getString("share");
        String baseDataId = baseDataoObject.getString("id");
        String preDate = baseDataoObject.getString("preDate");
        String packName = baseDataoObject.getString("packName");
        String phaseName = baseDataoObject.getString("phaseName");
        String subjectName = baseDataoObject.getString("subjectName");
        Date date = new Date(preDate);
        // 获取资源
        String resources = baseDataoObject.getString("resources");
        JSONArray resArray = JSONArray.fromObject(resources);
        List<String> resList = new ArrayList<String>();
        if (resArray.size() > 0) {
            for (int i = 0; i < resArray.size(); i++) {
                String resourceId = resArray.getJSONObject(i).getString("resourceId");
                String courseType = resArray.getJSONObject(i).getString("courseType");
                String formatId = resArray.getJSONObject(i).getString("formatId");
                String resourceUrl = resArray.getJSONObject(i).getString("resourceUrl");
                String orderNum = resArray.getJSONObject(i).getString("orderNum");
                String resourceName = resArray.getJSONObject(i).getString("resourceName");
                String usesTest = resArray.getJSONObject(i).getString("usesTest");
                String usesProview = resArray.getJSONObject(i).getString("usesProview");
                String usesTeaching = resArray.getJSONObject(i).getString("usesTeaching");
                String usesHomework = resArray.getJSONObject(i).getString("usesHomework");
                resList.add(resourceId);
                resList.add(courseType);
                resList.add(formatId);
                resList.add(orderNum);
                resList.add(resourceName);
                resList.add(usesTest);
                resList.add(usesProview);
                resList.add(usesTeaching);
                resList.add(usesHomework);
                resList.add(resourceUrl);
            }
        }

        // 保存基本数据
        YxyEsbagTeachingReport yxyEsbagTeachingReport = new YxyEsbagTeachingReport();
        yxyEsbagTeachingReport.setId(baseDataId);
        yxyEsbagTeachingReport.setPackName(packName);
        yxyEsbagTeachingReport.setTeacherId(userId);
        yxyEsbagTeachingReport.setTeacherNick(userNick);
        yxyEsbagTeachingReport.setPreDate(date);
        yxyEsbagTeachingReport.setBookcontentsId(bookcontentsId);
        yxyEsbagTeachingReport.setBookcontentsName(bookcontentsName);
        yxyEsbagTeachingReport.setSchoolId(schoolId);
        yxyEsbagTeachingReport.setPhaseId(phaseId);
        yxyEsbagTeachingReport.setPhaseName(phaseName);
        yxyEsbagTeachingReport.setSemesterId(semesterId);
        yxyEsbagTeachingReport.setSemesterName(semesterName);
        ;
        yxyEsbagTeachingReport.setSubjectId(subjectId);
        yxyEsbagTeachingReport.setSubjectName(subjectName);
        yxyEsbagTeachingReport.setVersionId(versionId);
        yxyEsbagTeachingReport.setVersionName(versionName);
        Long userTime1 = 0l;
        if (null != userTime && userTime != "null" && userTime.length() > 0) {
            userTime1 = Long.valueOf(userTime);
            yxyEsbagTeachingReport.setUserTime(userTime1);
        } else {
            userTime = 0 + "";
        }
        yxyEsbagTeachingReport.setClassId(classId);
        yxyEsbagTeachingReport.setClassName(className);
        yxyEsbagTeachingReportRepository.save(yxyEsbagTeachingReport);
        log.info("解析并保存基本数据成功,开始解析随堂笔记数据...");

        JSONObject noteTaking = jsonArray.getJSONObject(2);
        String noteTakingString = noteTaking.toString();
        JSONObject noteTakingJsonObject = JSONObject.fromObject(noteTakingString);
        // 获取课堂笔记对象
        String noteTakingObjString = noteTakingJsonObject.getString("noteTaking");
        JSONArray noteTakingaArray = JSONArray.fromObject(noteTakingObjString);
        if (noteTakingaArray.size() > 0) {
            List<Note> noteTakinglList = new ArrayList<Note>();

            String notPackId = "";
            if (noteTakingaArray.size() > 0) {
                for (int i = 0; i < noteTakingaArray.size(); i++) {
                    String noteTakingImgBase64 = noteTakingaArray.getJSONObject(i).getString("noteTakingImgBase64");
                    String isUpload = noteTakingaArray.getJSONObject(i).getString("isUpload");
                    String noteId = noteTakingaArray.getJSONObject(i).getString("_id");
                    String img = "";
                    String imageUrl = "";
                    if (noteTakingImgBase64.length() > 22) {
                        img = noteTakingImgBase64.substring(22);
                    }
                    // 上传图片
                    if (isUpload.equals("true")) {
                        String url = noteTakingaArray.getJSONObject(i).getString("url");
                        imageUrl = url;
                    } else {
                        imageUrl = getImageUrl(img, packId);
                    }
                    Note note = new Note();
                    note._id = noteId;
                    note.url = imageUrl;
                    noteTakinglList.add(note);
                }
                notPackId = noteTakingaArray.getJSONObject(0).getString("packId");
            }
            YxyEsbagTeachingNoteTaking esbagTeachingNoteTaking = null;

            esbagTeachingNoteTaking = new YxyEsbagTeachingNoteTaking();
            esbagTeachingNoteTaking.setTeacherId(userId);
            esbagTeachingNoteTaking.setId(packId + userId);
            esbagTeachingNoteTaking.setTeacherNick(userNick);
            esbagTeachingNoteTaking.setAvantar("");
            esbagTeachingNoteTaking.setSchoolId(schoolId);
            esbagTeachingNoteTaking.setSchoolNick(schoolNick);
            esbagTeachingNoteTaking.setClassName(className);
            esbagTeachingNoteTaking.setClassId(classId);
            esbagTeachingNoteTaking.setPreDate(date);
            esbagTeachingNoteTaking.setUserTime(userTime1);
            esbagTeachingNoteTaking.setPackId(notPackId);
            esbagTeachingNoteTaking.setNoteTaking(noteTakinglList);
            yxyEsbagTeachingNoteTakingRepository.save(esbagTeachingNoteTaking);
        }
        log.info("解析并保存随堂笔记成功,开始解析课堂表现数据...");

        JSONObject performance = jsonArray.getJSONObject(3);
        String stringPerformance = performance.toString();
        JSONObject performanceObject = JSONObject.fromObject(stringPerformance);
        // 获取课堂表现对象
        String performanceString = performanceObject.getString("performance");
        JSONArray performanceArray = JSONArray.fromObject(performanceString);
        if (performanceArray.size() > 0) {
            for (int i = 0; i < performanceArray.size(); i++) {
                // 获取对象属性并存入数据库
                String pId = performanceArray.getJSONObject(i).getString("id");
                String performancePackId = performanceArray.getJSONObject(i).getString("packId");
                String studentId = performanceArray.getJSONObject(i).getString("studentId");
                String studentNick = performanceArray.getJSONObject(i).getString("studentNick");
                String avantar = performanceArray.getJSONObject(i).getString("avatarUrl");
                String performance1 = performanceArray.getJSONObject(i).getString("performance");
                String rate = performanceArray.getJSONObject(i).getString("rate");
                String duration = performanceArray.getJSONObject(i).getString("duration");
                String isUpload = performanceArray.getJSONObject(i).getString("isUpload");
                // String status =
                // performanceArray.getJSONObject(i).getString("status");
                YxyEsbagTeachingPerformance esbagTeachingPerformance = new YxyEsbagTeachingPerformance();
                esbagTeachingPerformance.setId(pId);
                esbagTeachingPerformance.setPackId(performancePackId);
                esbagTeachingPerformance.setPackName(packName);
                esbagTeachingPerformance.setTeacherId(userId);
                // esbagTeachingPerformance.setStatus(Integer.valueOf(status));
                esbagTeachingPerformance.setTeacherNick(userNick);
                esbagTeachingPerformance.setStudentId(studentId);
                esbagTeachingPerformance.setStudentNick(studentNick);
                esbagTeachingPerformance.setAvantar(avantar);
                esbagTeachingPerformance.setPerformance(Integer.valueOf(performance1));
                esbagTeachingPerformance.setRate(Double.valueOf(rate));
                esbagTeachingPerformance.setDuration(Long.valueOf(duration));
                esbagTeachingPerformance.setSchoolId(schoolId);
                esbagTeachingPerformance.setSchoolNick(schoolNick);
                esbagTeachingPerformance.setPreDate(date);
                esbagTeachingPerformance.setUserTime(userTime1);
                esbagTeachingPerformance.setClassId(classId);
                esbagTeachingPerformance.setClassName(className);
                yxyEsbagTeachingPerformanceRepository.save(esbagTeachingPerformance);
            }
        }
        log.info("解析并保存课堂表现成功,开始解析测试题组数据...");
        JSONObject testQuesGroup = jsonArray.getJSONObject(4);
        String stringTestQuesGroup = testQuesGroup.toString();
        JSONObject testQuesGroupObject = JSONObject.fromObject(stringTestQuesGroup);
        // 获取测试题组对象
        String testQuesGroupString = testQuesGroupObject.getString("testQuesGroup");
        if (null != testQuesGroupString && "null" != testQuesGroupString && testQuesGroupString.length() > 0) {
            JSONArray testQuesGroupArray = JSONArray.fromObject(testQuesGroupString);
            for (int j = 0; j < testQuesGroupArray.size(); j++) {
                String idGroup = testQuesGroupArray.getJSONObject(j).getString("_id");
                String packIdGroup = testQuesGroupArray.getJSONObject(j).getString("packId");
                String quesGroupId = testQuesGroupArray.getJSONObject(j).getString("quesGroupId");
                String quesGroupName = testQuesGroupArray.getJSONObject(j).getString("quesGroupName");
                String quesTotal = testQuesGroupArray.getJSONObject(j).getString("quesTotal");
                String startTimeStamp = testQuesGroupArray.getJSONObject(j).getString("startTimeStamp");
                String varsion = testQuesGroupArray.getJSONObject(j).getString("version");

                YxyEsbagTeachingTestQuesGroup yxyEsbagTeachingTestQuesGroup = new YxyEsbagTeachingTestQuesGroup();
                yxyEsbagTeachingTestQuesGroup.setId(idGroup);
                yxyEsbagTeachingTestQuesGroup.setQuesGroupId(quesGroupId);
                yxyEsbagTeachingTestQuesGroup.setQuesGroupName(quesGroupName);
                yxyEsbagTeachingTestQuesGroup.setClassId(classId);
                yxyEsbagTeachingTestQuesGroup.setClassName(className);
                yxyEsbagTeachingTestQuesGroup.setPackName(packName);
                yxyEsbagTeachingTestQuesGroup.setPreDate(date);
                yxyEsbagTeachingTestQuesGroup.setQuesGroupImage(null);
                yxyEsbagTeachingTestQuesGroup.setSchoolId(schoolId);
                yxyEsbagTeachingTestQuesGroup.setSchoolNick(schoolNick);
                yxyEsbagTeachingTestQuesGroup.setTeacherId(userId);
                yxyEsbagTeachingTestQuesGroup.setTeacherNick(userNick);
                yxyEsbagTeachingTestQuesGroup.setUserTime(userTime1);
                yxyEsbagTeachingTestQuesGroup.setPackId(packIdGroup);
                esbagTeachingTestQuesGroupRepository.save(yxyEsbagTeachingTestQuesGroup);
            }
        }
        log.info("解析并保存测试题组成功,开始解析测试题组学生数据...");

        JSONObject testQuesGroupStu = jsonArray.getJSONObject(5);
        String stringTestQuesGroupStu = testQuesGroupStu.toString();
        JSONObject testQuesGroupObjectStu = JSONObject.fromObject(stringTestQuesGroupStu);
        // 获取测试题组对象
        String testQuesGroupStringStu = testQuesGroupObjectStu.getString("testQuesGroupStu");
        if (null != testQuesGroupStringStu && "null" != testQuesGroupStringStu && testQuesGroupStringStu.length() > 0) {
            JSONArray testQuesGroupArrayStu = JSONArray.fromObject(testQuesGroupStringStu);

            for (int j = 0; j < testQuesGroupArrayStu.size(); j++) {
                String idGroupStu = testQuesGroupArrayStu.getJSONObject(j).getString("_id");
                String studentIdGroupStu = testQuesGroupArrayStu.getJSONObject(j).getString("studentId");
                String studentNickGroupStu = testQuesGroupArrayStu.getJSONObject(j).getString("studentNick");
                String avantarGroupStu = testQuesGroupArrayStu.getJSONObject(j).getString("avantar");
                String quesGroupIdGroupStu = testQuesGroupArrayStu.getJSONObject(j).getString("quesGroupId");
                String quesGroupNameGroupStu = testQuesGroupArrayStu.getJSONObject(j).getString("quesGroupName");
                String varsion = testQuesGroupArrayStu.getJSONObject(j).getString("version");

                YxyEsbagTeachingTestQuesGroupStu e = new YxyEsbagTeachingTestQuesGroupStu();
                e.setId(idGroupStu);
                e.setStudentId(studentIdGroupStu);
                e.setStudentNick(studentNickGroupStu);
                e.setQuesGroupId(quesGroupIdGroupStu);
                e.setQuesGroupName(quesGroupNameGroupStu);
                // 空
                // e.setQuesGroupImage("");
                // e.setStatus(0);
                e.setPackName(packName);
                e.setTeacherId(userId);
                e.setTeacherNick(userNick);
                e.setSchoolId(schoolId);
                e.setSchoolNick(schoolNick);
                e.setPreDate(date);
                e.setUserTime(userTime1);
                e.setClassId(classId);
                e.setClassName(className);
                yxyEsbagTeachingTestQuesGroupStuRepository.save(e);
            }
        }

        log.info("解析并保存测试题组学生成功,开始解析测试试题数据...");

        JSONObject testQues = jsonArray.getJSONObject(6);
        String stringTestQues = testQues.toString();
        JSONObject testQuesObject = JSONObject.fromObject(stringTestQues);
        // 获取测试题组对象
        String testQuesString = testQuesObject.getString("testQues");
        if (null != testQuesString && "null" != testQuesString && testQuesString.length() > 0) {
            JSONArray testQuesArray = JSONArray.fromObject(testQuesString);

            for (int j = 0; j < testQuesArray.size(); j++) {

                String idTeseQues = testQuesArray.getJSONObject(j).getString("_id");
                String packIdTeseQues = testQuesArray.getJSONObject(j).getString("packId");
                // String avantarTeseQues =
                // testQuesArray.getJSONObject(j).getString("avantar");
                String quesGroupIdTeseQues = testQuesArray.getJSONObject(j).getString("quesGroupId");
                String quesGroupNameTeseQues = testQuesArray.getJSONObject(j).getString("quesGroupName");
                String varsionTeseQues = testQuesArray.getJSONObject(j).getString("version");

                YxyEsbagTeachingTestQues testQuesForSave = new YxyEsbagTeachingTestQues();
                testQuesForSave.setId(idTeseQues);
                testQuesForSave.setQuesGroupId(quesGroupIdTeseQues);
                // 空
                // testQuesForSave.setQuesId(testQues.quesId);
                // testQuesForSave.setKnowsId(testQues.knowsId);
                // testQuesForSave.setKnowsName(testQues.knowsName);
                testQuesForSave.setPackName(packName);
                testQuesForSave.setTeacherId(userId);
                testQuesForSave.setTeacherNick(userNick);
                testQuesForSave.setSchoolId(schoolId);
                testQuesForSave.setSchoolNick(schoolNick);
                testQuesForSave.setPreDate(date);
                testQuesForSave.setUserTime(userTime1);
                testQuesForSave.setClassId(classId);
                testQuesForSave.setClassName(className);
                esbagTeachingTestQuesRepository.save(testQuesForSave);
            }
        }
        log.info("解析并保存测试题组学生成功,开始解析测试学生试题数据...");
        JSONObject testQuesStu = jsonArray.getJSONObject(7);
        String stringTestQuesStu = testQuesStu.toString();
        JSONObject testQuesObjectStu = JSONObject.fromObject(stringTestQuesStu);
        // 获取测试题组对象
        String testQuesStringStu = testQuesObjectStu.getString("testQuesStu");
        if (null != testQuesStringStu && "null" != testQuesStringStu && testQuesStringStu.length() > 0) {
            JSONArray testQuesArrayStu = JSONArray.fromObject(testQuesStringStu);

            List<YxyEsbagTeachingTestQuesStu> listTesetQuesStus = new ArrayList<YxyEsbagTeachingTestQuesStu>();

            for (int j = 0; j < testQuesArrayStu.size(); j++) {

                String idTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("_id");
                String studentIdTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("studentId");
                String studentNickTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("studentNick");
                // String avantarTeseQuesStu =
                // testQuesArrayStu.getJSONObject(j).getString("avantar");
                String quesGroupIdTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("quesGroupId");
                String quesGroupNameTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("quesGroupName");
                String quesIdTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("quesId");
                String questionTypeTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("questionType");
                String answerOfMyTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("answerOfMy");
                String answerImgsTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("answerImgs");
                String answerTextTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("answerText");
                String answerOfQuesTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("answerOfQues");
                String passedTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("passed");
                String markTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("mark");
                String durationTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("duration");
                String knowsIdTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("knowsId");
                String knowsNameTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("knowsName");
                String wrongKnowIdLstTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("wrongKnowIdLst");
                String wrongKnowNameLstTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("wrongKnowNameLst");
                String exactnessKnowIdLstTeseQuesStu = testQuesArrayStu.getJSONObject(j)
                        .getString("exactnessKnowIdLst");
                String exactnessKnowNameLstTeseQuesStu = testQuesArrayStu.getJSONObject(j)
                        .getString("exactnessKnowNameLst");
                String rightRateTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("rightRate");
                String statusTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("status");
                String isReadOverTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("isReadOver");
                String varsionTeseQuesStu = testQuesArrayStu.getJSONObject(j).getString("version");

                YxyEsbagTeachingTestQuesStu yxyEsbagTeachingTestQuesStu = new YxyEsbagTeachingTestQuesStu();
                yxyEsbagTeachingTestQuesStu.setId(idTeseQuesStu);
                yxyEsbagTeachingTestQuesStu.setPackId(packId);
                yxyEsbagTeachingTestQuesStu.setStudentId(studentIdTeseQuesStu);
                yxyEsbagTeachingTestQuesStu.setStudentNick(studentNickTeseQuesStu);
                yxyEsbagTeachingTestQuesStu.setQuesGroupId(quesGroupIdTeseQuesStu);
                yxyEsbagTeachingTestQuesStu.setQuesId(quesIdTeseQuesStu);
                yxyEsbagTeachingTestQuesStu.setAnswerOfMy(answerOfMyTeseQuesStu);
                yxyEsbagTeachingTestQuesStu.setAnswerOfQues(answerOfQuesTeseQuesStu);
                yxyEsbagTeachingTestQuesStu.setQuesType(questionTypeTeseQuesStu);
                List<String> quesImage = new ArrayList<String>();
                String imgString = "";
                if (null != answerImgsTeseQuesStu && answerImgsTeseQuesStu.length() > 0
                        && !answerImgsTeseQuesStu.equals("null")) {
                    if (answerImgsTeseQuesStu.contains("http")) {
                        JSONArray jArray = JSONArray.fromObject(answerImgsTeseQuesStu);
                        for (int i = 0; i < jArray.size(); i++) {
                            String imageUrl = jArray.get(i).toString();
                            quesImage.add(imageUrl);
                        }
                    } else {
                        JSONArray jArray = JSONArray.fromObject(answerImgsTeseQuesStu);
                        for (int i = 0; i < jArray.size(); i++) {
                            String string2 = jArray.get(i).toString();
                            imgString = string2.substring(22);
                            String imageUrl = getImageUrl(imgString, packId);
                            quesImage.add(imageUrl);
                        }
                    }
                }
                yxyEsbagTeachingTestQuesStu.setQuesImage(quesImage);
                if (null != passedTeseQuesStu && !passedTeseQuesStu.equals("null")) {
                    yxyEsbagTeachingTestQuesStu.setPassed(Integer.valueOf(passedTeseQuesStu));

                }
                if (null != durationTeseQuesStu && durationTeseQuesStu.length() > 0
                        && !durationTeseQuesStu.equals("null")) {
                    yxyEsbagTeachingTestQuesStu.setDuration(Integer.valueOf(durationTeseQuesStu));
                }
                List<String> knowsIdTeseQuesStulList = new ArrayList<String>();
                if (null != knowsIdTeseQuesStu && knowsIdTeseQuesStu.length() > 0
                        && !knowsIdTeseQuesStu.equals("null")) {
                    knowsIdTeseQuesStu = knowsIdTeseQuesStu.substring(1, knowsIdTeseQuesStu.length() - 1);
                    String[] stringArra = knowsIdTeseQuesStu.split(",");
                    for (String string2 : stringArra) {
                        knowsIdTeseQuesStulList.add(string2);
                    }
                }
                yxyEsbagTeachingTestQuesStu.setKnowsId(knowsIdTeseQuesStulList);
                List<String> knowsNameTeseQuesStulList = new ArrayList<String>();
                if (null != knowsNameTeseQuesStu && knowsNameTeseQuesStu.length() > 0
                        && !knowsNameTeseQuesStu.equals("null")) {
                    knowsNameTeseQuesStu = knowsNameTeseQuesStu.substring(1, knowsNameTeseQuesStu.length() - 1);
                    String[] stringArrb = knowsNameTeseQuesStu.split(",");
                    for (String string2 : stringArrb) {
                        knowsNameTeseQuesStulList.add(string2);
                    }
                }
                yxyEsbagTeachingTestQuesStu.setKnowsName(knowsNameTeseQuesStulList);
                if (null != markTeseQuesStu && markTeseQuesStu.length() > 0 && !markTeseQuesStu.equals("null")) {
                    yxyEsbagTeachingTestQuesStu.setMark(Integer.valueOf(markTeseQuesStu));

                } else {
                    yxyEsbagTeachingTestQuesStu.setMark(0);
                }
                List<String> wrongKnowsNameTeseQuesStulList = new ArrayList<String>();
                if (null != wrongKnowNameLstTeseQuesStu && wrongKnowNameLstTeseQuesStu.length() > 0
                        && !wrongKnowNameLstTeseQuesStu.equals("null")) {
                    wrongKnowNameLstTeseQuesStu = wrongKnowNameLstTeseQuesStu.substring(1,
                            wrongKnowNameLstTeseQuesStu.length() - 1);
                    String[] stringArrc = wrongKnowNameLstTeseQuesStu.split(",");
                    for (String string2 : stringArrc) {
                        wrongKnowsNameTeseQuesStulList.add(string2);
                    }
                }
                yxyEsbagTeachingTestQuesStu.setWrongKnowName(wrongKnowsNameTeseQuesStulList);
                List<String> exactnessKnowsNameTeseQuesStulList = new ArrayList<String>();
                if (null != exactnessKnowNameLstTeseQuesStu && exactnessKnowNameLstTeseQuesStu.length() > 0
                        && !exactnessKnowNameLstTeseQuesStu.equals("null")) {
                    exactnessKnowNameLstTeseQuesStu = exactnessKnowNameLstTeseQuesStu.substring(1,
                            exactnessKnowNameLstTeseQuesStu.length() - 1);
                    String[] stringArrd = exactnessKnowNameLstTeseQuesStu.split(",");
                    for (String string2 : stringArrd) {
                        exactnessKnowsNameTeseQuesStulList.add(string2);
                    }
                }

                yxyEsbagTeachingTestQuesStu.setExactnessKnowName(exactnessKnowsNameTeseQuesStulList);
                yxyEsbagTeachingTestQuesStu.setRightRate(Integer.valueOf(rightRateTeseQuesStu));
                yxyEsbagTeachingTestQuesStu.setPackName(packName);
                yxyEsbagTeachingTestQuesStu.setTeacherId(userId);
                yxyEsbagTeachingTestQuesStu.setTeacherNick(userNick);
                yxyEsbagTeachingTestQuesStu.setSchoolId(schoolId);
                yxyEsbagTeachingTestQuesStu.setSchoolNick(schoolNick);
                yxyEsbagTeachingTestQuesStu.setPreDate(date);
                yxyEsbagTeachingTestQuesStu.setUserTime(userTime1);
                yxyEsbagTeachingTestQuesStu.setClassId(classId);
                yxyEsbagTeachingTestQuesStu.setClassName(className);
                yxyEsbagTeachingTestQuesStu.setIsReadOver(Integer.valueOf(isReadOverTeseQuesStu));
                yxyEsbagTeachingTestQuesStu.setStatus(Integer.valueOf(statusTeseQuesStu));
                listTesetQuesStus.add(yxyEsbagTeachingTestQuesStu);
                // 判断选择题正误
                listTesetQuesStus = lectureReportClassJudgeService.judgeChoiceQuestion(listTesetQuesStus);
            }
            yxyEsbagTeachingTestQuesStuRepository.save(listTesetQuesStus);
        }

        log.info("解析并保存测试学生试题成功,开始解析测试报告分析知识点数据...");

        JSONObject analyseKnow = jsonArray.getJSONObject(8);
        String stringAnalyseKnow = analyseKnow.toString();
        JSONObject testAnalyseKnow = JSONObject.fromObject(stringAnalyseKnow);
        // 获取知识点对象
        String analyseKnowString = testAnalyseKnow.getString("testReportAnalyseKnow");
        if (null != analyseKnowString && "null" != analyseKnowString && analyseKnowString.length() > 0) {
            JSONArray analyseKnowArray = JSONArray.fromObject(analyseKnowString);
            for (int i = 0; i < analyseKnowArray.size(); i++) {
                String idAnalyseKnow = analyseKnowArray.getJSONObject(i).getString("_id");
                String packIdAnalyseKnow = analyseKnowArray.getJSONObject(i).getString("packId");
                String knowsAnalyseKnow = analyseKnowArray.getJSONObject(i).getString("knowList");

                YxyEsbagTeachingTestAnalyseKnow yxyEsbagTeachingTestAnalyseKnow = new YxyEsbagTeachingTestAnalyseKnow();
                yxyEsbagTeachingTestAnalyseKnow.setPackName(packName);
                yxyEsbagTeachingTestAnalyseKnow.setTeacherId(userId);
                yxyEsbagTeachingTestAnalyseKnow.setTeacherNick(userNick);
                yxyEsbagTeachingTestAnalyseKnow.setSchoolId(schoolId);
                yxyEsbagTeachingTestAnalyseKnow.setSchoolNick(schoolNick);
                yxyEsbagTeachingTestAnalyseKnow.setPreDate(date);
                yxyEsbagTeachingTestAnalyseKnow.setUserTime(userTime1);
                yxyEsbagTeachingTestAnalyseKnow.setClassId(classId);
                yxyEsbagTeachingTestAnalyseKnow.setClassName(className);
                yxyEsbagTeachingTestAnalyseKnow.setId(idAnalyseKnow);
                List<Know> knows = new ArrayList<>();
                JSONArray knowsArray = JSONArray.fromObject(knowsAnalyseKnow);
                if (knowsArray.size() > 0) {
                    for (int j = 0; j < knowsArray.size(); j++) {
                        Know know = new Know();
                        // know.knowId =
                        // knowsArray.getJSONObject(j).getString("knowId");
                        know.knowName = knowsArray.getJSONObject(j).getString("knowName");
                        know.rate = Double.valueOf(knowsArray.getJSONObject(j).getString("rate"));
                        knows.add(know);
                    }
                }
                yxyEsbagTeachingTestAnalyseKnow.setKnows(knows);
                yxyEsbagTeachingTestAnalyseKnowRepository.save(yxyEsbagTeachingTestAnalyseKnow);
            }
        }
        log.info("解析并保存测试报告分析知识点成功,开始解析测试报告成绩数据...");
        JSONObject analyseScore = jsonArray.getJSONObject(9);
        String stringAnalyseScore = analyseScore.toString();
        JSONObject testAnalyseScore = JSONObject.fromObject(stringAnalyseScore);
        // 获取分析成绩对象
        String analyseScoreString = testAnalyseScore.getString("testReportAnalyseScore");
        if (null != analyseScoreString && "null" != analyseScoreString && analyseScoreString.length() > 0) {
            JSONArray analyseScoreArray = JSONArray.fromObject(analyseScoreString);
            for (int i = 0; i < analyseScoreArray.size(); i++) {
                String IdAnalyseScore = analyseScoreArray.getJSONObject(i).getString("_id");
                String rateForClassAnalyseScore = analyseScoreArray.getJSONObject(i).getString("rateForClass");
                String scoreRateAnalyseScore = analyseScoreArray.getJSONObject(i).getString("scoreRate");

                YxyEsbagTeachingTestAnalyseScore teaAnalyseScore = new YxyEsbagTeachingTestAnalyseScore();
                teaAnalyseScore.setPackName(packName);
                teaAnalyseScore.setTeacherId(userId);
                teaAnalyseScore.setTeacherNick(userNick);
                teaAnalyseScore.setSchoolId(schoolId);
                teaAnalyseScore.setSchoolNick(schoolNick);
                teaAnalyseScore.setPreDate(date);
                teaAnalyseScore.setUserTime(userTime1);
                teaAnalyseScore.setClassId(classId);
                teaAnalyseScore.setClassName(className);
                teaAnalyseScore.setId(IdAnalyseScore);
                teaAnalyseScore.setClassRate(Double.valueOf(rateForClassAnalyseScore));
                Map<Integer, Integer> scoreRate = new HashMap<Integer, Integer>();
                JSONArray knowsArray = JSONArray.fromObject(scoreRateAnalyseScore);
                if (knowsArray.size() > 0) {
                    for (int j = 0; j < knowsArray.size(); j++) {
                        String key = knowsArray.getJSONObject(j).getString("key");
                        String value = knowsArray.getJSONObject(j).getString("value");
                        scoreRate.put(Integer.valueOf(key), Integer.valueOf(value));
                    }
                    teaAnalyseScore.setScoreRate(scoreRate);
                }
                yxyEsbagTeachingTestAnalyseScoreRepository.save(teaAnalyseScore);
            }
        }
        log.info("解析完成");
    }

    /*
     * Base64字符串转 二进制流
     *
     * @param base64String Base64
     * 
     * @return base64String
     * 
     * @throws IOException 异常
     */
    private static byte[] getStringImage(String base64String) throws IOException {
        return base64String != null ? Base64.decodeBase64(base64String) : null;

    }

    // 上传图片
    private String getImageUrl(String img, String packId) throws IOException {
        // 上传图片获取图片url
        // 生成随机数
        Integer max = 10000;
        Integer min = 0;
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        String name = packId + s + ".jpg";
        String url = "";
        byte[] stringImage = null;

        try {
            stringImage = getStringImage(img);
            System.out.println(stringImage);
            // 上传图片的参数构造
            MultipartFile multipartFile = new MockMultipartFile("file", name, MediaType.APPLICATION_OCTET_STREAM_VALUE,
                    stringImage);

            url = yxcspUtil.push(multipartFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;

    }
}
